
import { PlayGround_Listen, UI_Layer, UI_Listen } from "../../config/ui_def"
import { getLongScreen } from '../tool/util'
cc.Class({
    extends: cc.Component,

    properties: {
        head_sp: cc.Sprite,
        coin_label: cc.Label,
        diamond_label: cc.Label,
        uid_label: cc.Label,

        coin_ani: 0,           //  动画暂存值
        coin_count: 0,          //  真实coin值
        change_coin: false     //  是否监听
    },
    initPage() {

    },
    updatePage() {
        this.widgt = this.node.getComponent(cc.Widget)
        gg.eventManager.on('topBar', this.listenClient, this)
        this.updateInfo()
        if (getLongScreen()) {
            this.widgt.top = 50
        } else {
            this.widgt.top = 0
        }
        let uid = he.local.get("uid", "user")
        this.uid_label.string = 'uid:' + uid
    },
    updateInfo() {
        // 基础积分
        let coin = he.local.get('coin', 'user')
        this.coin_ani = coin
        this.coin_count = coin
        this.coin_label.string = coin
        gg.eventManager.emit('topBar', UI_Listen.TOP_UPDATE_DIAMOND)

    },
    listenClient(code, param, arg) {
        if (code) {
            switch (code) {
                case UI_Listen.TOP_UPDATE_MONEY:
                    this.coin_count = he.local.get('coin', 'user')
                    this.coin_label.string = this.coin_count
                    this.change_coin = true
                    break
                case UI_Listen.TOP_UPDATE_DIAMOND:
                    let bag = he.local.getJson('bag', 'user')
                    let diamondNum = 0
                    if (bag && bag.length > 0) {
                        for (let i = 0; i < bag.length; i++) {
                            if (bag[i].id == 20002) {
                                diamondNum = bag[i].num
                                break
                            }
                        }
                    }
                    this.diamond_label.string = diamondNum
                    break
            }
        }
    },
    coinAni() {
        if (this.change_coin && this.coin_count != this.coin_ani) {
            if (this.coin_ani > this.coin_count) {
                this.coin_ani -= 1
            } else if (this.coin_ani < this.coin_count) {
                this.coin_ani += 1
            } else {
                this.change_coin = false
            }
        }
    },
    closeUI() {
        gg.eventManager.off('topBar', this.listenClient, this)
    },
    update(dt) {
        this.coinAni()
    },
});
